/**
 * Created by Fanjiang Huang on 2016/7/17.
 */

$(function(){

    //富文本文档样式兼容

    richText($(".rich-text span"));

    //一级导航
    var swiperLevel1 = new Swiper('.swiper-nav-level1', {
        slidesPerView: 4
    });

    //二级导航
    var swiperLevel2 = new Swiper('.swiper-nav-level2', {
        slidesPerView: 4
    });

    //三级导航
    var swiperLevel3 = new Swiper('.swiper-nav-level3', {
        slidesPerView: 4
    });

    //导航操作
    $('.swiper-nav-level1').hide();

    //$('.swiper-nav-level2').hide();

    $('.swiper-nav-level3').hide();


    var headerHeight = $('header').height(),
        navHeight = $('.book-second-nav').height();
    $('.swiper-nav-level1 .swiper-slide').click(function () {
        $(this).addClass('swiper-nav-level-on');
        $(this).siblings().removeClass('swiper-nav-level-on');
        if ($(this).hasClass('level')) {
            $('.swiper-nav-level2 .swiper-slide').removeClass('swiper-nav-level-other');
            $('.swiper-nav-level2').fadeIn(200);
            $('.swiper-nav-level3').fadeOut(200);
            $('section').css('padding-top',headerHeight + navHeight);
        } else {
            $('.swiper-nav-level2').fadeOut(200);
            $('.swiper-nav-level3').fadeOut(200);
            $('.swiper-nav-level2 .swiper-slide').removeClass('swiper-nav-level-other');
            $('.swiper-nav-level3 .swiper-slide').removeClass('swiper-nav-level-in');
            $('section').css('padding-top',headerHeight + navHeight);
        }
    });

    $('.swiper-nav-level2 .swiper-slide').click(function () {
        $(this).addClass('swiper-nav-level-other');
        $(this).siblings().removeClass('swiper-nav-level-other');
        $('.swiper-nav-level3 .swiper-slide').removeClass('swiper-nav-level-in');
        $('.swiper-nav-level3').fadeIn(200);
        if ($('.swiper-nav-level1').is(':hidden')) {
            $('section').css('padding-top',headerHeight + navHeight);
        } else {
            $('section').css('padding-top',headerHeight + navHeight*2);
        }
    });

    $('.swiper-nav-level3 .swiper-slide').click(function () {
        $(this).addClass('swiper-nav-level-in');
        $(this).siblings().removeClass('swiper-nav-level-in');
        $('.swiper-nav-level3').fadeIn(200);
    });

    //图片高度
    imgSize($('.service-book-img-main'));
});

$(window).resize(function () {
    imgSize($('.service-book-img-main'));
});

function imgSize (obj) {
    obj.each(function(){
        var imgWidth = $(this).width();
        $(this).height(imgWidth);
    });
}

function richText (obj) {
    obj.each(function () {
        var dataLevel = $('html').attr('data-dpr');
        var _this = $(this);
        var fontSize =  parseInt(_this.attr('style').match(/(?:font-size:)*(\d+\d*)/));

        if (dataLevel == 2) {
            _this.css('font-size',2*fontSize);
        } else if (dataLevel == 3) {
            _this.css('font-size',3*fontSize);
        }
    });
}
